package com.example.administrator.printoutindemo.mvp.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dothantech.lpapi.IAtBitmap;
import com.dothantech.lpapi.LPAPI;
import com.dothantech.printer.IDzPrinter;
import com.example.administrator.printoutindemo.R;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.zhinenghuacangku.mvp.ui.utils.PrinterUtil;
import com.xiaoxing.zhinenghuacangku_zhijian.mvp.ui.entity.GetUserSpaceList;

import java.util.List;

//import me.jessyan.armscomponent.commonres.utils.GlideUtil;


public class CangWeiLieBiaoAdapter extends BaseQuickAdapter<GetUserSpaceList, BaseViewHolder> {

    private Context mContext;
    private PrinterUtil mPrinterUtil;

    public CangWeiLieBiaoAdapter(Context context, @Nullable List<GetUserSpaceList> data) {
        super(R.layout.item_cang_wei_lie_biao, data);
        this.mContext = context;
        mPrinterUtil = new PrinterUtil(context);
        IDzPrinter.Factory.getInstance().init(context, mPrinterUtil.mCallback);
    }

    @Override
    protected void convert(BaseViewHolder helper, GetUserSpaceList item) {
        //GlideUtil.loadImage(mContext,item.getImg(),helper.getView(R.id.img_head));
        helper.setText(R.id.tv_cang_ku, item.getSname());
        helper.setText(R.id.tv_bh, item.getSpid());
        helper.setText(R.id.tv_cwmc, item.getSpname());
        helper.setText(R.id.tv_cwdz, item.getSpadd());
        helper.setText(R.id.tv_cwms, item.getSpdes());
        helper.setText(R.id.tv_cwbz, item.getSpremark());

        if (item.getSpstatus().equals("0")) {
            helper.setText(R.id.tv_cwzt, "未占");
        }
        if (item.getSpstatus().equals("1")) {
            helper.setText(R.id.tv_cwzt, "未满");
        }
        if (item.getSpstatus().equals("2")) {
            helper.setText(R.id.tv_cwzt, "已满");
        }

        helper.setOnClickListener(R.id.tv_rk, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPrinterUtil.selectPrinterCheck()) {
                    printBigQRcodeOriention(String.valueOf(item.getSpid()));
                } else {
                    ArmsUtils.makeText(mContext, "请先连接二维码打印机");
                }
            }
        });
    }

    private void printBigQRcodeOriention(String content) {

        // 得到 IDzPrinter 对象
        IDzPrinter printer = IDzPrinter.Factory.getInstance();
        // 连接配对的第一个打印机对象
        printer.connect(IDzPrinter.Factory.getFirstPrinter());
        // 创建 IAtBitmap 绘图对象
        IAtBitmap api = IAtBitmap.Factory.createInstance();
        // 开始绘图任务，传入参数(页面宽度, 页面高度)
        api.setItemHorizontalAlignment(LPAPI.ItemAlignment.LEFT);
        api.setItemOrientation(180);

        api.startJob(50 * 100, 50 * 100);//48, 50
//        api.setItemOrientation(180);

        // 开始一个页面的绘制，绘制二维码

        api.drawText("仓位码：" + content, 36 * 100, 12 * 100, 0, 0, 2 * 100, IAtBitmap.FontStyle.REGULAR, 0);
        api.draw2DQRCode(content, 14 * 100, 15 * 100, 20 * 100);//  9, 10, 20,30
        // 结束绘图任务
        api.endJob();
        // 打印
        printer.print(api, null);

    }
}
